﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Javavaj.iHunter;
namespace Javavaj.iHunter.Viewers
{
    public class DefaultViewer :IViewer
    {
        #region IViewer 成员

        public string[] DocumentTypes
        {
            get { return null; }
        }

        public System.Windows.Forms.Control CreateControl()
        {
            return new DefaultViewerControl();
        }

        public void ShowDocument(System.Windows.Forms.Control control,string docType, Stream doc)
        {
            DefaultViewerControl viewControl = control as DefaultViewerControl;
            if (viewControl == null)
                return;
            string tempFile = Path.GetTempFileName();
            string tempPath = Path.Combine(
                Path.GetDirectoryName(tempFile),
                Path.GetFileNameWithoutExtension(tempFile)
                ) + "." + docType;
                
            try
            {
                byte[] buffer = new byte[1024];
                using (Stream temp = File.OpenWrite(tempPath))
                {
                    while (true)
                    {
                        int nRead = doc.Read(buffer, 0, 1024);
                        if (nRead == 0)
                            break;
                        temp.Write(buffer, 0, nRead);
                    }
                }
                viewControl.Disposed += delegate
                {
                    try
                    {
                        File.Delete(tempPath);
                    }
                    catch
                    {
                    }
                };

                viewControl.ShowDocument(tempPath);

            }
            catch
            {
            }
        }

        public void PrintDocument(System.Drawing.Graphics g)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IPlugin 成员

        public void InitSettings(System.Xml.XmlDocument settings)
        {
        }

        public void Config()
        {
        }

        public event Javavaj.iHunter.Core.PluginSettingChangedEventHandler SettingChanged;

        #endregion
    }
}
